package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.codeless.bo.PageCompButtonUpdateBO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.PageCompButtonUpdateDao;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class PageCompButtonUpdateService {
    @Resource
    private PageCompButtonUpdateDao pageCompButtonUpdateDao;
    
    public PageCompButtonUpdateBO queryBy(String id) {
        return pageCompButtonUpdateDao.queryBy(id);
    }
    
    public List<PageCompButtonUpdateBO> queryList(PageCompButtonUpdateBO bo) {
        return pageCompButtonUpdateDao.queryList(bo);
    }
    
    public PageInfo<PageCompButtonUpdateBO> queryPage(PageCompButtonUpdateBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(pageCompButtonUpdateDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(PageCompButtonUpdateBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return pageCompButtonUpdateDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return pageCompButtonUpdateDao.insert(bo);
        }
    }

    @WriteTransactional
    public int save(String buttonId, List<PageCompButtonUpdateBO> list, String employeeId) {
        if (Tool.isBlank(buttonId)) {
            return 0;
        }

        for (PageCompButtonUpdateBO update : list) {
            update.setButtonId(buttonId);
            if (Tool.isBlank(update.getId())) {
                update.setId(UUID.getUUID());
                update.setUpdateId(employeeId);
                update.setCreateId(employeeId);
                pageCompButtonUpdateDao.insert(update);
            } else {
                update.setUpdateId(employeeId);
                pageCompButtonUpdateDao.update(update);
            }
        }

        // 删除被删除的数据
        PageCompButtonUpdateBO qo = new PageCompButtonUpdateBO();
        qo.setButtonId(buttonId);
        pageCompButtonUpdateDao.queryList(qo).stream()
                .filter(update -> list.stream().noneMatch(temp -> update.getId().equals(temp.getId())))
                .forEach(this::delete);

        return 1;
    }

    @WriteTransactional
    public void saveList(List<PageCompButtonUpdateBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (pageCompButtonUpdateDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                pageCompButtonUpdateDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                pageCompButtonUpdateDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public int delete(PageCompButtonUpdateBO bo) {
        return pageCompButtonUpdateDao.delete(bo);
    }

    @WriteTransactional
    public void deleteByButtonId(String buttonId) {
        if (Tool.isBlank(buttonId)) {
            return;
        }

        PageCompButtonUpdateBO qo = new PageCompButtonUpdateBO();
        qo.setButtonId(buttonId);
        pageCompButtonUpdateDao.queryList(qo).forEach(this::delete);
    }

    public List<PageCompButtonUpdateBO> queryByButtonId(String buttonId) {
        PageCompButtonUpdateBO qo = new PageCompButtonUpdateBO();
        qo.setButtonId(buttonId);
        return pageCompButtonUpdateDao.queryList(qo);
    }

    @WriteTransactional
    public void copyByButtonId(String oldButtonId, String newButtonId, String employeeId) {
        queryByButtonId(oldButtonId).forEach(update -> {
            update.setId(UUID.getUUID());
            update.setButtonId(newButtonId);
            update.setUpdateId(employeeId);
            update.setCreateId(employeeId);
            update.setUpdateTime(System.currentTimeMillis());
            update.setCreateTime(System.currentTimeMillis());
            pageCompButtonUpdateDao.insert(update);
        });
    }
}
